package de.akabahn.ebd.control.auth;

import java.util.HashMap;

import android.text.TextUtils;

/**
 * @author Christian Schlehuber
 * @author Peter Eimann
 * @since 0.1
 * @version 0.2
 */
public class StaticAuthenticator extends AbstractAuthenticator {

	private static StaticAuthenticator instance;
	private HashMap<String, UserAuthObject> userList;

	private StaticAuthenticator() {
		userList = new HashMap<String, UserAuthObject>();
		populateUserList();
	}

	private void populateUserList() {
		userList.put("restricted", new UserAuthObject("restricted", 
						"7ad91593df9293a8942f7438cb88f7ab1a8e354da491f567f9939b1303920f5", 
						AccessEnum.RESTRICTED, null));
		userList.put("common", new UserAuthObject("common", 
						"92a5dc4bd6f9fb8f29f8066fed8a5c1e81bc59ad48a11283b63736867e4f2a8", 
						AccessEnum.COMMON, null));
		userList.put("active", new UserAuthObject("active", 
						"9687961165f80a81392a52edb9b023766987c4518e1c130e541a55e0eeef", 
						AccessEnum.ACTIVE, null));
		userList.put("technician", new UserAuthObject("technician", 
						"29ba9d9cef5a66461116a24938bb937e05c35aa1bb909f16aa5e85bd767480", 
						AccessEnum.TECHNICIAN, null));
	}

	public static StaticAuthenticator getInstance() {
		if (instance == null) {
			instance = new StaticAuthenticator();
		}
		
		return instance;
	}

	@Override
	public UserAuthObject authenticateUser(String user, String password) {
		if (TextUtils.isEmpty(user) && TextUtils.isEmpty(password)) {
			return new UserAuthObject("Gast", null, AccessEnum.RESTRICTED, null);
		} else {
			UserAuthObject uaObject = userList.get(user);
			if (uaObject != null && uaObject.getPassword().equals(md5hash(password))) {
				return uaObject;
			} else {
				return null;
			}
		}
	}

	@Override
	public boolean changePassword(String user, String oldPassword, String newPassword) {
		return false;
	}
}
